package thesis.prototype;

import java.util.ArrayList;

import thesis.prototype.objects.ConnectPHP;
import thesis.prototype.objects.Meeting;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ViewMeetingsActivity extends Activity implements OnClickListener{

	// DB connectivity
	private ConnectPHP DBMethods;
	
	// UI elements
	private Button b_meets_next;
	private Button b_meets_cancel;
	
	// List 
	private ListView l_meetings;
	private ListAdapter l_listadapter;
	private ArrayList<Meeting> arr_meeting;
	private Meeting selectedMeeting;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.view_meetings);
		
		b_meets_next = (Button) findViewById(R.id.viewmeets_next);
		b_meets_cancel = (Button) findViewById(R.id.viewmeets_cancel);
		
		arr_meeting = new ArrayList<Meeting>();
		selectedMeeting = null;
		DBMethods = ConnectPHP.getInstance();
		
		addButtonListeners();
		refreshMeetingsList();
	}
	
	private void addButtonListeners()
	{
		if (b_meets_next != null) b_meets_next.setOnClickListener(this);
		if (b_meets_cancel != null) b_meets_cancel.setOnClickListener(this);
	}
	
	@SuppressWarnings("unchecked")
	private void refreshMeetingsList()
	{
		arr_meeting = DBMethods.getMeetings(HelloActivity.ownNumber);
		selectedMeeting = null;
        if (l_meetings == null) 
        {
    		l_meetings = (ListView) findViewById(R.id.meetinglist);
    		l_listadapter = new ArrayAdapter<Meeting>(this, android.R.layout.simple_list_item_single_choice, arr_meeting);
        	l_meetings.setAdapter(l_listadapter);
        	l_meetings.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        	l_meetings.setOnItemClickListener(new OnItemClickListener()
        	{
        	    public void onItemClick(AdapterView<?> parent, View view,
        	    		int position, long id)
        	    {
        	    	selectedMeeting = (Meeting) l_meetings.getItemAtPosition(position);
        	    }
        	});
        }
        else
        {
        	((ArrayAdapter<Meeting>)(l_meetings.getAdapter())).notifyDataSetChanged();
        	l_meetings.invalidate();
        }
	}
	
	@Override
	public void onClick(View v) {
		if(v==b_meets_cancel)
		{
			setResult(RESULT_CANCELED);
			finish();
		}
		else if(v==b_meets_next)
		{
			if(selectedMeeting!=null)
			{
				HelloActivity.viewing_meeting = selectedMeeting;
				Intent i = new Intent(this, DetailsActivity.class);
				startActivityForResult(i, HelloActivity.MEETING_DETAILS_INTENT);
			}		
		}
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data); 
		switch(requestCode) {
		case (HelloActivity.MEETING_DETAILS_INTENT):
			if (resultCode == Activity.RESULT_OK) // finished with everything
			{
				HelloActivity.viewing_meeting = null;
				setResult(RESULT_OK);
				finish();
			}
			else if (resultCode == Activity.RESULT_CANCELED) // just pressed Back to list
			{
				// Do nothing
			}
			break; 
		}
	}
}
